﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a component of a trigonometric polynomial.
    /// </summary>
    [Serializable]
    public class TrigonometricPolynomialComponent : ITrigonometricPolynomialComponent
    {
        /// <summary>
        /// The first coefficient of the trigonometric polynomial component.
        /// </summary>
        private double a;

        /// <summary>
        /// The second coefficient of the trigonometric polynomial component.
        /// </summary>
        private double b;

        /// <summary>
        /// Initializes a new instance of the <see cref="TrigonometricPolynomialComponent"/> class.
        /// </summary>
        /// <param name="a">The first coefficient of the trigonometric polynomial component.</param>
        /// <param name="b">The second coefficient of the trigonometric polynomial component.</param>
        public TrigonometricPolynomialComponent(double a, double b)
        {
            this.a = a;
            this.b = b;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TrigonometricPolynomialComponent"/> class.
        /// </summary>
        /// <param name="component">The trigonometric polynomial component to clone.</param>
        public TrigonometricPolynomialComponent(TrigonometricPolynomialComponent component)
        {
            if (component == (TrigonometricPolynomialComponent) null)
            {
                throw new ArgumentNullException("component");
            }

            this.a = component.A;
            this.b = component.B;
        }

        /// <summary>
        /// Gets or sets the first coefficient of the trigonometric polynomial component.
        /// </summary>
        /// <value>The first coefficient of the trigonometric polynomial component.</value>
        public double A
        {
            get { return a; }
            set { a = value; }
        }

        /// <summary>
        /// Gets or sets the second coefficient of the trigonometric polynomial component.
        /// </summary>
        /// <value>The second coefficient of the trigonometric polynomial component.</value>
        public double B
        {
            get { return b; }
            set { b = value; }
        }

        /// <summary>
        /// Solves the component at the specified position x.
        /// </summary>
        /// <param name="x">The value for evaluation.</param>
        /// <param name="k">The index of the component in the whole trigonometric polynomial.</param>
        /// <returns>The result t(x) at the specified position x.</returns>
        public double SolveAt(double x, int k)
        {
            return this.a * Math.Cos(k * x) + this.b * Math.Sin(k * x);
        }

        /// <summary>
        /// Copies the current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public TrigonometricPolynomialComponent Copy()
        {
            return new TrigonometricPolynomialComponent(this.a, this.b);
        }

        /// <summary>
        /// Compares the current instance of a component to another one.
        /// </summary>
        /// <param name="compare">The component to compare.</param>
        /// <returns>True if the components are even otherwise, false.</returns>
        public bool CompareTo(TrigonometricPolynomialComponent compare)
        {
            return this == compare;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial component.</param>
        /// <param name="b">The second trigonometric polynomial component.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(TrigonometricPolynomialComponent a, TrigonometricPolynomialComponent b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.A == b.A) && (a.B == b.B));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial component.</param>
        /// <param name="b">The second trigonometric polynomial component.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(TrigonometricPolynomialComponent a, TrigonometricPolynomialComponent b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial component.</param>
        /// <param name="b">The second trigonometric polynomial component.</param>
        /// <returns>The result of the operator.</returns>
        public static TrigonometricPolynomialComponent operator +(
            TrigonometricPolynomialComponent a, TrigonometricPolynomialComponent b)
        {
            if (a == (TrigonometricPolynomialComponent) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (TrigonometricPolynomialComponent) null)
            {
                throw new ArgumentNullException("b");
            }

            return new TrigonometricPolynomialComponent(a.A + b.A, a.B + b.B);
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial component.</param>
        /// <param name="b">The second trigonometric polynomial component.</param>
        /// <returns>The result of the operator.</returns>
        public static TrigonometricPolynomialComponent operator -(
            TrigonometricPolynomialComponent a, TrigonometricPolynomialComponent b)
        {
            if (a == (TrigonometricPolynomialComponent) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (TrigonometricPolynomialComponent) null)
            {
                throw new ArgumentNullException("b");
            }

            return new TrigonometricPolynomialComponent(a.A - b.A, a.B - b.B);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="k">The index of the component in the whole trigonometric polynomial.</param>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public string ToString(int k)
        {
            return this.a.ToString() + " cos(" + k.ToString() + "x) + " + this.b.ToString() + " sin(" + k.ToString() +
                   "x)";
        }
    }
}